import Datastore from 'nedb-promises'
import {
  InsertData, DeleteData, UpdateData, SearchData,
  InsertDataSync, DeleteDataSync, UpdateDataSync, SearchDataSync,
  SelectDatabase,
} from '@/utils/db'
import { SortArrayByKeys, FilterArrayByKeys } from '@/utils/tools'
import { customizeDictionaryDB } from './customizeDictionaryDB'
import type { CustomizeDictionaryValueType } from './customizeDictionaryDB'

const dictionaryDB = SelectDatabase('bug-manager', 'system', 'Dictionary')

/**
 * 添加字典
 * @param request
 * @param response
 */
const insertDictionary = async (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  const {
    dictionaryValue, // 英文
  } = queryParams

  const _db = dictionaryDB

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  const insertDataCallback = (searchResult: ResponseDataType) => {
    const { code = -1, total = -1 } = searchResult
    if (code > 0 && total < 1) {
      InsertData(_db, queryParams, resJSON, sendData)
    } else {
      resJSON.note = `字典值${dictionaryValue}已存在`
      sendData(resJSON)
    }
  }

  // 判断字典是否存在
  SearchData(_db, { dictionaryValue }, resJSON, insertDataCallback)
}

/**
 * 获取字典列表
 * @param request
 * @param response
 */
const getAllDictionary = async (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = dictionaryDB

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  // 判断字典是否存在
  SearchData(_db, {}, resJSON, sendData)
}

/**
 * 获取单个字典值
 */
const getDictionary = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const { dictionaryValue } = queryParams

  const _db = dictionaryDB

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, { dictionaryValue }, resJSON, sendData)
}

/**
 * 获取单个字典数组
 */
const getDictionaryArray = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }
  const {
    filterKeyList = ['label'],
    keyword = '',
    sort = [{ key: 'value', order: 1 }],
    pageSize = 5,
    current = 1,
    paging = true,
    extendsInfo = undefined
  } = queryParams

  queryParams.paging = false
  queryParams.keyword = ''
  queryParams.sort = []
  queryParams.filterKeyList = []

  if (extendsInfo) {
    queryParams[`${extendsInfo.key}`] = { $in: extendsInfo.value }
    delete queryParams.extendsInfo
  }

  const _db = dictionaryDB

  const sendData = (res: ResponseDataType) => {
    const { code = -1, total = -1 } = res
    if (code > 0 && total > 0) {
      let _dictionaryArray = FilterArrayByKeys(res.data[0].dictionaryArray || [], filterKeyList, keyword)
      res.total = _dictionaryArray.length
      if (paging) {
        res.data = SortArrayByKeys(_dictionaryArray, sort).slice((current - 1) * pageSize, current * pageSize)
      } else {
        res.data = SortArrayByKeys(_dictionaryArray, sort)
      }
    }
    resolve(res)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

/**
 * 获取自定义字典数组
 */
const getCustomizeDictionaryArray = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }
  const {
    filterKeyList = ['label'],
    keyword = '',
    sort = [{ key: 'value', order: 1 }],
    pageSize = 5,
    current = 1,
    paging = true,
    dictionaryValue,
    extendsInfo = undefined
  }= queryParams

  queryParams.paging = false
  queryParams.keyword = ''
  queryParams.sort = []
  queryParams.filterKeyList = []

  let extendsParams: any = {}

  if (extendsInfo) {
    extendsParams[`${extendsInfo.key}`] = { $in: extendsInfo.value }
    delete queryParams.extendsInfo
  }

  delete queryParams.dictionaryValue

  let _dictionaryValue: CustomizeDictionaryValueType = dictionaryValue

  if (!_dictionaryValue) {
    resJSON.note = '字典值不能为空'
    resolve(resJSON)
    return
  }
  if (!customizeDictionaryDB[_dictionaryValue]) {
    resJSON.note = '对应的字典数据不存在'
    resolve(resJSON)
    return
  }

  const _db = SelectDatabase('bug-manager', customizeDictionaryDB[_dictionaryValue].module, _dictionaryValue)

  const sendData = (res: ResponseDataType) => {
    const { code = -1, total = -1 } = res
    if (code > 0 && total > 0) {
      let _dictionaryArray = FilterArrayByKeys(res.data || [], filterKeyList, keyword)
      _dictionaryArray = customizeDictionaryDB[_dictionaryValue].getData(_dictionaryArray)
      res.total = _dictionaryArray.length
      if (paging) {
        res.data = SortArrayByKeys(_dictionaryArray, sort).slice((current - 1) * pageSize, current * pageSize)
      } else {
        res.data = SortArrayByKeys(_dictionaryArray, sort)
      }
    }
    resolve(res)
  }

  SearchData(_db, { ...extendsParams }, resJSON, sendData)
}


export default [
  // 系统字典
  {
    path: '/bugManager/system/insertDictionary',
    func: insertDictionary
  },
  {
    path: '/bugManager/system/getAllDictionary',
    func: getAllDictionary
  },
  {
    path: '/bugManager/system/getDictionary',
    func: getDictionary
  },
  {
    path: '/bugManager/system/getDictionaryArray',
    func: getDictionaryArray
  },
  {
    path: '/bugManager/system/getCustomizeDictionaryArray',
    func: getCustomizeDictionaryArray
  }
]